package com.lh.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lh.commonutils.R;
import com.lh.commonutils.ordervo.CourseWebVoOrder;
import com.lh.dao.EduCourseMapper;
import com.lh.entity.EduChapter;
import com.lh.entity.EduCourse;
import com.lh.entity.chapter.ChapterVo;
import com.lh.entity.frontvo.CourseQueryVo;
import com.lh.entity.frontvo.CourseWebVo;
import com.lh.service.IEduChapterService;
import com.lh.service.IEduCourseService;
import com.lh.service.IEduTeacherService;
import com.sun.xml.internal.ws.client.sei.ResponseBuilder;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.jws.Oneway;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/eduservice/coursefront")
public class CourseFrontController {
    @Autowired
    private IEduChapterService chapterService;

    @Resource
    private EduCourseMapper courseMapper;
    @Autowired
    private IEduCourseService eduCourseService;

    @ApiOperation(value = "分页课程列表")
    @PostMapping(value = "{page}/{limit}")
    public R pageList(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,

            @ApiParam(name = "courseQuery", value = "查询对象", required = false)
            @RequestBody(required = false) CourseQueryVo courseQuery){
        Page<EduCourse> pageParam = new Page<EduCourse>(page, limit);
        Map<String, Object> map = eduCourseService.pageListWeb(pageParam, courseQuery);
        return  R.ok().data(map);
    }

    @ApiOperation(value = "根据ID查询课程")
    @GetMapping(value = "{courseId}")
    public R getById(
            @ApiParam(name = "courseId", value = "课程ID", required = true)
            @PathVariable String courseId){

        //查询课程信息和讲师信息
        CourseWebVo courseWebVo = courseMapper.selectInfoWebById(courseId);

        //查询当前课程的章节信息
        R chapterVideo = chapterService.getChapterVideo(courseId);

        return R.ok().data("course", courseWebVo).data("chapterVoList", chapterVideo);
    }

//    根据课程id查询课程信息
    @ApiOperation(value = "根据课程id查询课程信息")
    @PostMapping("getCourseInfoOrder/{id]")
    public CourseWebVoOrder  getCourseInfoOrder(@PathVariable String id){
        CourseWebVo courseWebVo = eduCourseService.selectInfoWebById(id);
        CourseWebVoOrder courseWebVoOrder = new CourseWebVoOrder();
        BeanUtils.copyProperties(courseWebVo,courseWebVoOrder);
        return courseWebVoOrder;
    }

}
